<template>
  <div class="app-container">
    <el-row :gutter="20">
        <!--工具栏-->
      <el-card>
        <div class="head-container">
          <div v-if="crud.props.searchToggle">
            <!-- 搜索 -->
            <el-input v-model="query.keyword" clearable size="small" placeholder="请输入大学名称" style="width: 200px;"
              class="filter-item" @keyup.enter.native="crud.toQuery" />
            <el-select clearable class="filter-item" :remote="true" v-model="query.eduType" @change="crud.toQuery"
              filterable placeholder="请选择层次">
              <el-option v-for="item in eduTypes" :key="item.id" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
            <el-select clearable class="filter-item" :remote="true" v-model="query.enrollmentType" @change="crud.toQuery"
              filterable placeholder="请选择招生类型">
              <el-option v-for="item in examTypes" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
   

            <el-cascader placeholder="请选择城市" :options="regionData" :props="{ children: 'child' }" class="filter-item" @change="cityChange"
              v-model="selectedOptions">
            </el-cascader>
            <rrOperation />
            <el-button class="filter-item" size="mini" type="primary" icon="el-icon-plus" @click="add">
              新增
            </el-button>

          </div>
          <crudOperation show="" :permission="permission" />
        </div>
      </el-card>
      <el-card style="margin-top:20px;">
        <!--表格渲染-->
        <el-table ref="table" v-loading="crud.loading" :data="crud.data" style="width: 100%;"
          @selection-change="crud.selectionChangeHandler">
          <el-table-column label="校徽">
            <template slot-scope="scope">
              <el-image style="width:120px;height:120px;" fit="cover" lazy :src="scope.row.emblem" class="n_avatar" :previewSrcList="[scope.row.emblem]" />
            </template>
          </el-table-column>
          <el-table-column :show-overflow-tooltip="true" prop="uniName" label="名称" />
          <el-table-column prop="city" label="城市" />
          <el-table-column :show-overflow-tooltip="true" prop="address" label="地址" />
          <el-table-column label="操作" align="center" width="270">
            <template slot-scope="scope">
              <el-button class="btn" icon="el-icon-edit" type="primary" size="mini" @click="edit(scope.row)">编辑</el-button>
              <el-button class="btn" icon="el-icon-edit" type="primary" size="mini" @click="toDetail(scope.row)">详情</el-button>
              <!-- <el-button class="btn" :loading="scope.row.del_loading" icon="el-icon-delete" type="danger" size="mini" @click="del(scope.row)"></el-button> -->
            </template>
          </el-table-column>
        </el-table>
      </el-card>
        <!--分页组件-->
        <pagination />
    </el-row>
    <schoolAddDialog ref="schoolAddDialog" @ok="crud.toQuery()"></schoolAddDialog>
  </div>
</template>

<script>
import crudSchool from '@/api/web/school'
import CRUD, {
  presenter,
  header,
  form,
  crud
} from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import DateRangePicker from '@/components/DateRangePicker'
import schoolAddDialog from './modual/schoolAddDialog';
import regionData from '@/utils/area';
export default {
  name: 'exam',
  components: {
    crudOperation,
    rrOperation,
    udOperation,
    pagination,
    DateRangePicker,
    schoolAddDialog
  },
  cruds() {
    return CRUD({
      title: '用户',
      url: 'api/uni/list',
      methodStr: 'post',
      dataStr: ['data', 'uniList'],
      query:{
        uniType:4
      },
      optShow: {
        reset: false,
        // othoer: true,
      },
      crudMethod: { ...crudSchool }
    })
  },
  mixins: [presenter(), header(), crud()],
  data() {
    // 自定义验证
    const validPhone = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入电话号码'))
      } else if (!isvalidPhone(value)) {
        callback(new Error('请输入正确的11位手机号码'))
      } else {
        callback()
      }
    }
    return {
      height: document.documentElement.clientHeight - 180 + 'px;',
      regionData,
      selectedOptions: [],
      eduTypes: [
        { label: '专科', value: 1 },
        { label: '本科', value: 2 },

      ],
      examTypes: [
                { label: '统考', value: 1 },
                { label: '校考', value: 2 },

            ],
      defaultProps: {
        children: 'children',
        label: 'name',
        isLeaf: 'leaf'
      },
      permission: {
        add: ['admin', 'user:add'],
        edit: ['admin', 'article:edit'],
        del: ['admin', 'user:del']
      },
      
      uniTypes: [
        { label: '停招院校', value: 1 },
        { label: '新增院校', value: 2 },
        { label: '特殊院校', value: 3 },
        { label: '校考院校', value: 4 },

      ],
    }
  },
  mounted: function () {
    const that = this
    window.onresize = function temp() {
      that.height = document.documentElement.clientHeight - 180 + 'px;'
    }
  },
  methods: {
    add() {
      this.query.selectedOptions = this.selectedOptions
      this.$refs.schoolAddDialog.add(this.query)
    },
    edit(item) {
      this.$refs.schoolAddDialog.edit(item)
    },
    toDetail(item) {
      this.$router.push('/mini/school/add?id=' + item.uniId)
    },
    cityChange() {

      if (this.selectedOptions) {
        this.$set(this.query, 'cityCode', this.selectedOptions[this.selectedOptions.length - 1])
      } else {
        this.$set(this.query, 'cityCode', '')
      }
      this.crud.toQuery()
    },
    del(item) {
      this.$confirm(`文章删除后不可恢复，确认删除?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$set(item, 'del_loading', true)
        crudArticle.del(item.id).then(res => {
          this.$set(item, 'del_loading', false)
          if (res.code == 200) {
            this.$message({
              type: 'success',
              message: res.message
            })
            this.crud.toQuery()
          } else {
            this.$message({
              type: 'error',
              message: res.message
            })
          }
        })
      })

    },
    changeStatus(item, type, loadingStr) {
      let confirmContent = ''
      let confirmStatus = false
      if (type == 1) {
        if (item.ifRecommend) {
          confirmContent = '取消推荐'
        } else {
          confirmContent = '推荐'
        }
        confirmStatus = !item.ifRecommend
      } else {
        if (item.ifSelected) {
          confirmContent = '取消精选'
        } else {
          confirmContent = '精选'
        }
        confirmStatus = !item.ifSelected
      }
      this.$confirm(`是否${confirmContent}该文章`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$set(item, loadingStr, true)
        crudArticle.changeStatus({ id: item.id, type: type, status: confirmStatus }).then(res => {
          this.$set(item, loadingStr, false)
          if (res.code == 200) {
            this.$message({
              type: 'success',
              message: res.message
            })
            this.crud.toQuery()
          } else {
            this.$message({
              type: 'arror',
              message: res.message
            })
          }
        }).catch(e => {
          this.$set(item, loadingStr, false)
        })
      })

    },
    // 禁止输入空格
    keydown(e) {
      if (e.keyCode === 32) {
        e.returnValue = false
      }
    },
    checkboxT(row, rowIndex) {
      return row.id !== this.user.id
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .vue-treeselect__control,
::v-deep .vue-treeselect__placeholder,
::v-deep .vue-treeselect__single-value {
  height: 30px;
  line-height: 30px;
}

.n_avatar {
  max-width: 120px;
  max-height: 120px;
}

.btn {
  margin: 0;
}
</style>
